package org.example.chapter1;

/**
 * 原理：
 * 每个线程Thread都会在内部保存一个ThreadLocalMap,key是ThreadLocal、value是threadLocal里的值
 */
public class S111_ThreadLocal {

    static ThreadLocal<String> threadLocal = new ThreadLocal<>();

    public static void main(String[] args) {
        Thread aaa = new Thread(() -> {
            threadLocal.set("hello from thread-aaa!");
            System.out.println(threadLocal.get());// 打印：hello from thread-aaa!
        });
        Thread bbb = new Thread(() -> {
            threadLocal.set("hello from thread-bbb!");
            System.out.println(threadLocal.get());// 打印：hello from thread-bbb!
        });

        aaa.start();
        bbb.start();

//        threadLocal.set("hello from thread-main!");
        System.out.println(threadLocal.get()); // 打印：null
    }


}
